package com.qiji.video.service.impl;

import com.qiji.video.mapper.extend.VideosExtendMapper;
import com.qiji.video.service.IUserActionService;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.sql.Timestamp;

import static com.qiji.video.util.RedisConstants.*;

@Service
public class UserActionServiceImpl implements IUserActionService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private VideosExtendMapper videosExtendMapper;

    @Override
    public Result likeVideo(Integer id, Integer vid) {

        String key = Video_LIKED_KEY + vid;
        Double score = stringRedisTemplate.opsForZSet().score(key,id.toString());

        if (score == null){
            boolean isSuccess = videosExtendMapper.updateLikesByIdAdd(vid);
            if (isSuccess){
                stringRedisTemplate.opsForZSet().add(key,id.toString(),System.currentTimeMillis());
            }
        }
        else{
            boolean isSuccess = videosExtendMapper.updateLikesByIdReduce(vid);
            if (isSuccess){
                stringRedisTemplate.opsForZSet().remove(key,id.toString());
            }
        }

        return ResultUtil.success();
    }
    //添加播放量
    @Override
    public Result addClicks(Integer userId, Integer vid) {
        // 判读是否存在是否存在观看历史，如果有则更新观看时间，不存在这添加

        Integer isSuccess = videosExtendMapper.selectISBrowsByUVId(vid);
        if (isSuccess.equals(0)){
            videosExtendMapper.addBrowsByUVId(userId,vid,new Timestamp(System.currentTimeMillis()));
        }
        else{
            videosExtendMapper.updateBrowsByUVId(userId,vid,new Timestamp(System.currentTimeMillis()));
        }
        videosExtendMapper.updateClicksById(vid);

        // 把播放量存储到redis
        long hour = System.currentTimeMillis()/(1000 * 60 * 60);

        String key = Video_Click_KEY_HOUR + hour;
        Double score = stringRedisTemplate.opsForZSet().score(key,vid.toString());
        if (score == null){
            this.stringRedisTemplate.opsForZSet().add(key,vid.toString(),1);
        }
        else{
            this.stringRedisTemplate.opsForZSet().remove(key,vid.toString());
            this.stringRedisTemplate.opsForZSet().add(key,vid.toString(),score+1);
        }

        return ResultUtil.success();
    }
    //添加收藏
    @Override
    public Result collectionVideo(Integer userId, Integer vid) {

        //判断是否存在收藏，则取消收藏
        Integer isSuccess = videosExtendMapper.selectIsCollectByUVId(userId,vid);

        if (isSuccess.equals(0)){
            videosExtendMapper.addCollectByUVId(userId,vid,new Timestamp(System.currentTimeMillis()));
        }
        else{
            videosExtendMapper.deleteCollectByUVId(userId,vid);
        }
        return ResultUtil.success();
    }
}
